package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.dao.AplicativoDAO;
import view.chart.AcaoChart;
import view.chart.AplicativoChart;
import view.chart.CenarioChart;
import view.chart.TarefaChart;
import view.chart.TelaChart;

import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JTextFieldDateEditor;

public class MainScreen extends JFrame{
	private static final long serialVersionUID = -2090091310033923483L;
	private static final int MIN_WIDTH = 900;
	private static final int MIN_HEIGHT = 720;
	
	private static final int LEFT_MARGIM = 15;
	private static final int TOP_MARGIM = 15;
	private static final int HORIZONTAL_MARGIM = 30;
	private static final int VERTICAL_MARGIM = 5;
	
	private static final String FONT_NAME = "Verdana";
	
	private static final String APPLICATION_TITLE = "Log - TVDi";
	private static final String APPLICATION_COMBO_LABEL = "Selecione a aplicação: ";
	private static final String START_PERIOD__LABEL = "Período inicial: ";
	private static final String END_PERIOD_LABEL = "Período final: ";
	
	private JPanel mainPanel;
	private JLabel applicationComboLabel;
	private JComboBox<String> applicationComboBox;
	private JLabel startPeriodLabel;
	private JLabel endPeriodLabel;
	private JDateChooser startPeriodDateChooser;
	private JDateChooser endPeriodDateChooser;
	private JPanel titlePanel;
	private JToggleButton filtroToggleButton;
	private JTabbedPane filtroTabbedPane;
	private List<JToggleButton> toggleButtons;
	private JPanel togglesPanel;
	private JTabbedPane graphTabbedPane;
	private JPanel bodyPanel;
	
	private String currentApplication;
	private String startPeriod;
	private String endPeriod;
	private int currentToggleIndex;
	private HashMap<Integer, Integer> tabSelectedIndex;
		
	public MainScreen(){
		this.getContentPane().setPreferredSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
		this.pack();
		this.setMinimumSize(this.getSize());
		//this.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
		this.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth())/2, this.getY());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle(APPLICATION_TITLE);
		
		setCurrentToggleIndex(0);
				
		this.setLayout(null);
		getMainPanel().setLocation(LEFT_MARGIM, TOP_MARGIM);
		this.getContentPane().add(getMainPanel());
		
		this.addComponentListener(new ComponentAdapter(){
			@Override
			public void componentResized(ComponentEvent e){
				getMainPanel().setSize(getContentPane().getSize().width - 2 * LEFT_MARGIM, getContentPane().getSize().height - 2 * TOP_MARGIM);
				getMainPanel().setLocation(LEFT_MARGIM, TOP_MARGIM);
			}
		});
	}
	
	private JPanel getMainPanel(){
		if(mainPanel == null){
			mainPanel = new JPanel();
			mainPanel.setLayout(null);
			mainPanel.setSize(this.getContentPane().getSize().width - 2 * LEFT_MARGIM, this.getContentPane().getSize().height - 2 * TOP_MARGIM);
			
			getTitlePanel().setLocation(0, 0);
			getBodyPanel().setLocation(0, getTitlePanel().getY() + getTitlePanel().getHeight() + TOP_MARGIM);
			mainPanel.add(getTitlePanel());
			mainPanel.add(getBodyPanel());
			
			mainPanel.addComponentListener(new ComponentAdapter(){
				@Override
				public void componentResized(ComponentEvent e){
					getTitlePanel().setSize(getMainPanel().getSize().width, getTitlePanel().getHeight());
					getTitlePanel().setLocation(0, 0);
					getBodyPanel().setSize(getMainPanel().getWidth(), getMainPanel().getHeight() - (getTitlePanel().getY() + getTitlePanel().getHeight() + TOP_MARGIM));
					getBodyPanel().setLocation(0, getTitlePanel().getY() + getTitlePanel().getHeight() + TOP_MARGIM);
				}
			});
		}
		return mainPanel;
	}
	
	private JPanel getTitlePanel(){
		if(titlePanel == null){
			titlePanel = new JPanel();
			titlePanel.setLayout(null);
			titlePanel.setSize(getMainPanel().getWidth(), getApplicationComboLabel().getHeight() + VERTICAL_MARGIM + getApplicationComboBox().getHeight());
			
			getApplicationComboLabel().setLocation(0, 0);
			getApplicationComboBox().setLocation(getApplicationComboLabel().getX(), getApplicationComboLabel().getY() + getApplicationComboLabel().getHeight() + VERTICAL_MARGIM);
			
			getStartPeriodLabel().setLocation(Math.max(getApplicationComboLabel().getX() + getApplicationComboLabel().getWidth() + HORIZONTAL_MARGIM, getApplicationComboBox().getX() + getApplicationComboBox().getWidth() + HORIZONTAL_MARGIM), getApplicationComboLabel().getY());
			getStartPeriodDateChooser().setLocation(getStartPeriodLabel().getX(), getApplicationComboBox().getY());
			getEndPeriodLabel().setLocation(Math.max(getStartPeriodLabel().getX() + getStartPeriodLabel().getWidth() + HORIZONTAL_MARGIM, getStartPeriodDateChooser().getX() + getStartPeriodDateChooser().getWidth() + HORIZONTAL_MARGIM), getApplicationComboLabel().getY());
			getEndPeriodDateChooser().setLocation(getEndPeriodLabel().getX(), getApplicationComboBox().getY());
			
			getFiltroToggleButton().setLocation(titlePanel.getWidth() - getFiltroToggleButton().getWidth(), getApplicationComboBox().getY());
			
			titlePanel.add(getApplicationComboLabel());
			titlePanel.add(getApplicationComboBox());
			titlePanel.add(getStartPeriodLabel());
			titlePanel.add(getStartPeriodDateChooser());
			titlePanel.add(getEndPeriodLabel());
			titlePanel.add(getEndPeriodDateChooser());
			titlePanel.add(getFiltroToggleButton());
			
			titlePanel.addComponentListener(new ComponentAdapter(){
				@Override
				public void componentResized(ComponentEvent e){
					getApplicationComboLabel().setLocation(0, 0);
					getApplicationComboBox().setLocation(getApplicationComboLabel().getX(), getApplicationComboLabel().getY() + getApplicationComboLabel().getHeight() + VERTICAL_MARGIM);
					
					getStartPeriodLabel().setLocation(Math.max(getApplicationComboLabel().getX() + getApplicationComboLabel().getWidth() + HORIZONTAL_MARGIM, getApplicationComboBox().getX() + getApplicationComboBox().getWidth() + HORIZONTAL_MARGIM), getApplicationComboLabel().getY());
					getStartPeriodDateChooser().setLocation(getStartPeriodLabel().getX(), getApplicationComboBox().getY());
					getEndPeriodLabel().setLocation(Math.max(getStartPeriodLabel().getX() + getStartPeriodLabel().getWidth() + HORIZONTAL_MARGIM, getStartPeriodDateChooser().getX() + getStartPeriodDateChooser().getWidth() + HORIZONTAL_MARGIM), getApplicationComboLabel().getY());
					getEndPeriodDateChooser().setLocation(getEndPeriodLabel().getX(), getApplicationComboBox().getY());
					
					getFiltroToggleButton().setLocation(titlePanel.getWidth() - getFiltroToggleButton().getWidth(), getApplicationComboBox().getY());
				}
			});
		}
		return titlePanel;
	}
	
	private JPanel getBodyPanel(){
		if(bodyPanel == null){
			bodyPanel = new JPanel();
			bodyPanel.setLayout(null);
			bodyPanel.setSize(getMainPanel().getWidth(), getMainPanel().getHeight() - (getTitlePanel().getY() + getTitlePanel().getHeight() + TOP_MARGIM));
			
			getTogglesPanel().setLocation(0, 0);
			changeGraphTabbedPane(getCurrentToggleIndex());
			bodyPanel.add(getTogglesPanel());
						
			bodyPanel.addComponentListener(new ComponentAdapter(){
				@Override
				public void componentResized(ComponentEvent e){
					getTogglesPanel().setLocation(0, 0);
					getGraphTabbedPane().setSize(getBodyPanel().getWidth() - (getTogglesPanel().getWidth() + LEFT_MARGIM), getBodyPanel().getHeight());
					getGraphTabbedPane().setLocation(getTogglesPanel().getX() + getTogglesPanel().getWidth() + LEFT_MARGIM, getTogglesPanel().getY());
				}
			});
		}
		return bodyPanel;
	}
	
	private JLabel getApplicationComboLabel(){
		if(applicationComboLabel == null){
			applicationComboLabel = new JLabel(APPLICATION_COMBO_LABEL);
			applicationComboLabel.setFont(new Font(FONT_NAME, Font.PLAIN, 12));
			applicationComboLabel.setSize(applicationComboLabel.getPreferredSize());
		}
		return applicationComboLabel;
	}
	
	private JComboBox<String> getApplicationComboBox(){
		if(applicationComboBox == null){
			try{
				applicationComboBox = new JComboBox<String>();				
				List<String> aplicacoes = AplicativoDAO.queryDistinctNomes();
				for(int i=0; i<aplicacoes.size(); i++){
					applicationComboBox.addItem(aplicacoes.get(i));
				}
				applicationComboBox.setSelectedIndex(-1);
				applicationComboBox.setFont(new Font(FONT_NAME, Font.ITALIC | Font.BOLD, 14));				
				applicationComboBox.setSize(applicationComboBox.getPreferredSize().width, applicationComboBox.getPreferredSize().height);
				((JLabel)getApplicationComboBox().getRenderer()).setHorizontalAlignment(SwingConstants.LEFT);
				
				applicationComboBox.addItemListener(new ItemListener(){
					@Override
					public void itemStateChanged(ItemEvent ie){
						if(ie.getStateChange() == ItemEvent.SELECTED){
							setCurrentApplication((String)ie.getItem());
							changeGraphTabbedPane(getCurrentToggleIndex());
						}
					}
				});
			}catch(NullPointerException e){
				e.printStackTrace();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		return applicationComboBox;
	}

	private JLabel getStartPeriodLabel(){
		if(startPeriodLabel == null){
			startPeriodLabel = new JLabel(START_PERIOD__LABEL);
			startPeriodLabel.setFont(new Font(FONT_NAME, Font.PLAIN, 12));
			startPeriodLabel.setSize(startPeriodLabel.getPreferredSize());
		}
		return startPeriodLabel;
	}
	
	private JLabel getEndPeriodLabel(){
		if(endPeriodLabel == null){
			endPeriodLabel = new JLabel(END_PERIOD_LABEL);
			endPeriodLabel.setFont(new Font(FONT_NAME, Font.PLAIN, 12));
			endPeriodLabel.setSize(endPeriodLabel.getPreferredSize());
		}
		return endPeriodLabel;
	}
	
	private JDateChooser getStartPeriodDateChooser(){
		if(startPeriodDateChooser == null){
			try{
				Date startDate = AplicativoDAO.queryDataPrimeiroRegistro();
				startPeriodDateChooser = new JDateChooser(startDate, "dd/MM/yyyy");
				startPeriodDateChooser.setSelectableDateRange(startDate, new Date());
				JTextFieldDateEditor jText = ((JTextFieldDateEditor)startPeriodDateChooser.getComponent(1));
				jText.setFont(new Font(FONT_NAME, Font.BOLD, 12));
				jText.setDisabledTextColor(Color.BLACK);
				jText.setEnabled(false);				
				startPeriodDateChooser.setSize(startPeriodDateChooser.getComponent(0).getPreferredSize().width + jText.getFontMetrics(jText.getFont()).stringWidth(jText.getText()) + 6, startPeriodDateChooser.getPreferredSize().height);

				startPeriodDateChooser.addPropertyChangeListener("date", new PropertyChangeListener(){
					public void propertyChange(PropertyChangeEvent evt){
						setStartPeriod(startPeriodDateChooser.getJCalendar().getYearChooser().getYear() + "-" + (startPeriodDateChooser.getJCalendar().getMonthChooser().getMonth() + 1) + "-" + startPeriodDateChooser.getJCalendar().getDayChooser().getDay());
						changeGraphTabbedPane(getCurrentToggleIndex());
					}
				});
				setStartPeriod(startPeriodDateChooser.getJCalendar().getYearChooser().getYear() + "-" + (startPeriodDateChooser.getJCalendar().getMonthChooser().getMonth() + 1) + "-" + startPeriodDateChooser.getJCalendar().getDayChooser().getDay());
			}catch(NullPointerException e){
				e.printStackTrace();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		return startPeriodDateChooser;
	}
	
	private JDateChooser getEndPeriodDateChooser(){
		if(endPeriodDateChooser == null){
			try{
				Date startDate = AplicativoDAO.queryDataPrimeiroRegistro();
				Date currentDate = new Date();
				endPeriodDateChooser = new JDateChooser(currentDate, "dd/MM/yyyy");
				endPeriodDateChooser.setSelectableDateRange(startDate, currentDate);
				JTextFieldDateEditor jText = ((JTextFieldDateEditor)endPeriodDateChooser.getComponent(1));
				jText.setFont(new Font(FONT_NAME, Font.BOLD, 12));
				jText.setDisabledTextColor(Color.BLACK);
				jText.setEnabled(false);				
				endPeriodDateChooser.setSize(endPeriodDateChooser.getComponent(0).getPreferredSize().width + jText.getFontMetrics(jText.getFont()).stringWidth(jText.getText()) + 6, endPeriodDateChooser.getPreferredSize().height);
				
				endPeriodDateChooser.addPropertyChangeListener("date", new PropertyChangeListener(){
					public void propertyChange(PropertyChangeEvent evt){
						setEndPeriod(endPeriodDateChooser.getJCalendar().getYearChooser().getYear() + "-" + (endPeriodDateChooser.getJCalendar().getMonthChooser().getMonth() + 1) + "-" + endPeriodDateChooser.getJCalendar().getDayChooser().getDay());
						changeGraphTabbedPane(getCurrentToggleIndex());
					}
				});
				setEndPeriod(endPeriodDateChooser.getJCalendar().getYearChooser().getYear() + "-" + (endPeriodDateChooser.getJCalendar().getMonthChooser().getMonth() + 1) + "-" + endPeriodDateChooser.getJCalendar().getDayChooser().getDay());
			}catch(NullPointerException e){
				e.printStackTrace();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		return endPeriodDateChooser;
	}
	
	private JToggleButton getFiltroToggleButton(){
		if(filtroToggleButton == null){
			filtroToggleButton = new JToggleButton("Filtrar dados...");
			filtroToggleButton.setSize(filtroToggleButton.getPreferredSize());
			filtroToggleButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					if(((JToggleButton)e.getSource()).isSelected()){
						getFiltroTabbedPane().setLocation(getBodyPanel().getLocation());
						getMainPanel().remove(getBodyPanel());
						getMainPanel().add(getFiltroTabbedPane());
						getMainPanel().repaint();
					}else{
						getMainPanel().remove(getFiltroTabbedPane());
						getMainPanel().add(getBodyPanel());
						getMainPanel().repaint();
					}
				}
			});
		}
		return filtroToggleButton;
	}
	
	private JTabbedPane getFiltroTabbedPane(){
		if(filtroTabbedPane == null){
			filtroTabbedPane = new JTabbedPane();
			filtroTabbedPane.setSize(getMainPanel().getWidth(), getMainPanel().getHeight() - (getTitlePanel().getY() + getTitlePanel().getHeight() + TOP_MARGIM));
			filtroTabbedPane.addTab("TESTE", new JLabel("Teste..."));
		}
		return filtroTabbedPane;
	}
	
	private List<JToggleButton> getToggleButtons(){
		if(toggleButtons == null){
			toggleButtons = new ArrayList<JToggleButton>();
			toggleButtons.add(new JToggleButton("Aplicação"));
			toggleButtons.add(new JToggleButton("Cenários"));
			toggleButtons.add(new JToggleButton("Tarefas"));
			toggleButtons.add(new JToggleButton("Telas"));
			toggleButtons.add(new JToggleButton("Ações"));
			toggleButtons.get(getCurrentToggleIndex()).setSelected(true);
			
			int width = 0, height = 0;
			for(int i=0; i<toggleButtons.size(); i++){
				if(toggleButtons.get(i).getPreferredSize().width > width){
					width = toggleButtons.get(i).getPreferredSize().width;
				}if(toggleButtons.get(i).getPreferredSize().height > height){
					height = toggleButtons.get(i).getPreferredSize().height;
				}
			}
			
			for(int i=0; i<toggleButtons.size(); i++){
				toggleButtons.get(i).setSize(width, height);
				
				final int index = i;
				toggleButtons.get(i).addActionListener(new ActionListener(){
					@Override
					public void actionPerformed(ActionEvent ae){
						if(toggleButtons.get(index).isSelected()){
							for(int j=0; j<toggleButtons.size(); j++){
								if(j != index){
									toggleButtons.get(j).setSelected(false);
								}
							}
							setCurrentToggleIndex(index);
						}else{
							toggleButtons.get(index).setSelected(true);
						}
						changeGraphTabbedPane(getCurrentToggleIndex());
					}
				});
			}
		}
		return toggleButtons;
	}
	
	private JPanel getTogglesPanel(){
		if(togglesPanel == null){
			togglesPanel = new JPanel();
			togglesPanel.setLayout(null);
			togglesPanel.setSize(getToggleButtons().get(getCurrentToggleIndex()).getWidth(), (toggleButtons.get(getCurrentToggleIndex()).getHeight() + VERTICAL_MARGIM) * toggleButtons.size());
			
			for(int i=0; i<getToggleButtons().size(); i++){
				toggleButtons.get(i).setLocation(0, (toggleButtons.get(i).getHeight() + VERTICAL_MARGIM) * i);
				togglesPanel.add(toggleButtons.get(i));
			}
		}
		return togglesPanel;		
	}

	private JTabbedPane getGraphTabbedPane(){
		return graphTabbedPane;
	}
	
	private void setGraphTabbedPane(){
		graphTabbedPane = new JTabbedPane();
		graphTabbedPane.setSize(getBodyPanel().getWidth() - (getTogglesPanel().getX() + getTogglesPanel().getWidth() + LEFT_MARGIM), getBodyPanel().getHeight());
		graphTabbedPane.setLocation(getTogglesPanel().getX() + getTogglesPanel().getWidth() + LEFT_MARGIM, getTogglesPanel().getY());
	}
	
	private void changeGraphTabbedPane(int currentToggleIndex){
		System.out.println("CHANGE GRAPH TAB PANE[" + currentToggleIndex + "]>> " + getCurrentApplication() + ", " + getStartPeriod() + ", " + getEndPeriod());
		if(getGraphTabbedPane() != null){
			getBodyPanel().remove(getGraphTabbedPane());
		}
		setGraphTabbedPane();
		getBodyPanel().add(getGraphTabbedPane());
		
		if(currentToggleIndex == 0){ // Aplicação
			getGraphTabbedPane().addTab("Quantidade de Acesso", null);
			getGraphTabbedPane().addTab("Tempo Médio", null);
		}else if(currentToggleIndex == 1){ // Cenário
			getGraphTabbedPane().addTab("Quantidade por Usuário", null);
			getGraphTabbedPane().addTab("Tempo Médio", null);
			getGraphTabbedPane().addTab("Percentual de Tempo Médio", null);
		}else if(currentToggleIndex == 2){ // Tarefas
			getGraphTabbedPane().addTab("Quantidade por Usuário", null);
			getGraphTabbedPane().addTab("Tempo Médio", null);
			getGraphTabbedPane().addTab("Percentual de Tempo Médio", null);
		}else if(currentToggleIndex == 3){ // Telas
			getGraphTabbedPane().addTab("Quantidade de Acesso", null);
			getGraphTabbedPane().addTab("Percentual de Acesso", null);
			getGraphTabbedPane().addTab("Tempo Médio", null);
			getGraphTabbedPane().addTab("Percentual de Tempo Médio", null);
		}else if(currentToggleIndex == 4){ // Ações
			getGraphTabbedPane().addTab("Quantidade por Aplicativo", null);
			getGraphTabbedPane().addTab("Quantidade por Usuário", null);
		}
		
		getGraphTabbedPane().setSelectedIndex(getTabSelectedIndex(getCurrentToggleIndex()));
		changeTabs(currentToggleIndex, graphTabbedPane.getSelectedIndex());
		
		getGraphTabbedPane().addChangeListener(new ChangeListener(){
			@Override
			public void stateChanged(ChangeEvent e){
				setTabSelectedIndex(getCurrentToggleIndex(), graphTabbedPane.getSelectedIndex());			
				changeTabs(getCurrentToggleIndex(), graphTabbedPane.getSelectedIndex());
			}
		});
	}
	
	private void changeTabs(int currentToggleIndex, int currentTabIndex){
		System.out.println("CHANGE TABS[" + currentToggleIndex + ", " + currentTabIndex + "]>> " + getCurrentApplication() + ", " + getStartPeriod() + ", " + getEndPeriod());
		if(currentToggleIndex == 0){
			if(getTabSelectedIndex(getCurrentToggleIndex()) == 0){
				getGraphTabbedPane().setComponentAt(getTabSelectedIndex(getCurrentToggleIndex()), AplicativoChart.createQuantidadeAcessoPanel(getCurrentApplication(), getStartPeriod(), getEndPeriod()));
			}else if(getTabSelectedIndex(getCurrentToggleIndex()) == 1){
				getGraphTabbedPane().setComponentAt(getTabSelectedIndex(getCurrentToggleIndex()), AplicativoChart.createTempoMedioPanel(getCurrentApplication(), getStartPeriod(), getEndPeriod()));
			}
		}else if(currentToggleIndex == 1){
			if(getTabSelectedIndex(getCurrentToggleIndex()) == 0){
				getGraphTabbedPane().setComponentAt(getTabSelectedIndex(getCurrentToggleIndex()), CenarioChart.createQuantidadePorUsuarioPanel(getCurrentApplication(), getStartPeriod(), getEndPeriod()));
			}else if(getTabSelectedIndex(getCurrentToggleIndex()) == 1){
				getGraphTabbedPane().setComponentAt(getTabSelectedIndex(getCurrentToggleIndex()), CenarioChart.createTempoMedioPanel(getCurrentApplication(), getStartPeriod(), getEndPeriod()));
			}else if(getTabSelectedIndex(getCurrentToggleIndex()) == 2){
				getGraphTabbedPane().setComponentAt(getTabSelectedIndex(getCurrentToggleIndex()), CenarioChart.createTempoMedioPiePanel(getCurrentApplication(), getStartPeriod(), getEndPeriod()));
			}
		}else if(currentToggleIndex == 2){
			if(getTabSelectedIndex(getCurrentToggleIndex()) == 0){
				getGraphTabbedPane().setComponentAt(getTabSelectedIndex(getCurrentToggleIndex()), TarefaChart.createQuantidadePorUsuarioPanel(getCurrentApplication(), getStartPeriod(), getEndPeriod()));
			}else if(getTabSelectedIndex(getCurrentToggleIndex()) == 1){
				getGraphTabbedPane().setComponentAt(getTabSelectedIndex(getCurrentToggleIndex()), TarefaChart.createTempoMedioPanel(getCurrentApplication(), getStartPeriod(), getEndPeriod()));
			}else if(getTabSelectedIndex(getCurrentToggleIndex()) == 2){
				getGraphTabbedPane().setComponentAt(getTabSelectedIndex(getCurrentToggleIndex()), TarefaChart.createTempoMedioPiePanel(getCurrentApplication(), getStartPeriod(), getEndPeriod()));
			}
		}else if(currentToggleIndex == 3){
			if(getTabSelectedIndex(getCurrentToggleIndex()) == 0){
				getGraphTabbedPane().setComponentAt(getTabSelectedIndex(getCurrentToggleIndex()), TelaChart.createQuantidadeAcessoPanel(getCurrentApplication(), getStartPeriod(), getEndPeriod()));
			}else if(getTabSelectedIndex(getCurrentToggleIndex()) == 1){
				getGraphTabbedPane().setComponentAt(getTabSelectedIndex(getCurrentToggleIndex()), TelaChart.createQuantidadeAcessoPiePanel(getCurrentApplication(), getStartPeriod(), getEndPeriod()));
			}else if(getTabSelectedIndex(getCurrentToggleIndex()) == 2){
				getGraphTabbedPane().setComponentAt(getTabSelectedIndex(getCurrentToggleIndex()), TelaChart.createTempoMedioPanel(getCurrentApplication(), getStartPeriod(), getEndPeriod()));
			}else if(getTabSelectedIndex(getCurrentToggleIndex()) == 3){
				getGraphTabbedPane().setComponentAt(getTabSelectedIndex(getCurrentToggleIndex()), TelaChart.createTempoMedioPiePanel(getCurrentApplication(), getStartPeriod(), getEndPeriod()));
			}
		}else if(currentToggleIndex == 4){
			if(getTabSelectedIndex(getCurrentToggleIndex()) == 0){
				getGraphTabbedPane().setComponentAt(getTabSelectedIndex(getCurrentToggleIndex()), AcaoChart.createQuantidadePorAplicativoPanel(getCurrentApplication(), getStartPeriod(), getEndPeriod()));
			}else if(getTabSelectedIndex(getCurrentToggleIndex()) == 1){
				getGraphTabbedPane().setComponentAt(getTabSelectedIndex(getCurrentToggleIndex()), AcaoChart.createQuantidadePorUsuarioPanel(getCurrentApplication(), getStartPeriod(), getEndPeriod()));
			}
		}
	}
	
	private String getCurrentApplication(){
		return currentApplication;
	}
	
	private void setCurrentApplication(String currentApplication){
		this.currentApplication = currentApplication;
	}
	
	private String getStartPeriod(){
		return startPeriod;
	}
	
	private void setStartPeriod(String startPeriod){
		this.startPeriod = startPeriod;
	}
	
	private String getEndPeriod(){
		return endPeriod;
	}
	
	private void setEndPeriod(String endPeriod){
		this.endPeriod = endPeriod;
	}
	
	private int getCurrentToggleIndex(){
		return currentToggleIndex;
	}
	
	private void setCurrentToggleIndex(int currentToggleIndex){
		this.currentToggleIndex = currentToggleIndex;
	}
	
	private HashMap<Integer, Integer> getTabSelectedIndex(){
		if(tabSelectedIndex == null){
			tabSelectedIndex = new HashMap<Integer, Integer>();
			for(int i=0; i<getToggleButtons().size(); i++){
				tabSelectedIndex.put(i, 0);				
			}
		}
		return tabSelectedIndex;
	}
	
	private int getTabSelectedIndex(int currentToggleIndex){
		return getTabSelectedIndex().get(currentToggleIndex);
	}
	
	private void setTabSelectedIndex(int currentToggleIndex, int value){
		getTabSelectedIndex().put(currentToggleIndex, value);
	}
	
	public static void main(String[] args){
		MainScreen mainScreen = new MainScreen();
		mainScreen.setVisible(true);
	}
}